package com.runfast.waimai.web.controller.custom;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastCustomService;
import com.runfast.waimai.service.WaiMaiRunfastAgentbusinessService;
import com.runfast.waimai.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * ${DESCRIPTION}客服管理
 *
 * @author chenzheng
 * @create 2018-05-15 20:20
 **/
@RestController("customController")
@RequestMapping("/server/custom")
public class CustomController {
    @Autowired
    private RunfastCustomService runfastCustomService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService waiMaiRunfastAgentbusinessService;
    /**
     * 客服列表带分页的
     */
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable, String agentIds){

        RunfastCustomExample example=new RunfastCustomExample();
        RunfastCustomExample.Criteria exampleCriteria=example.createCriteria();
        /**代理商多选*/
        if(StringUtils.isNotBlank(agentIds)){
            List<String> result = Arrays.asList(StringUtils.split(agentIds,","));
            if(result.size()>0){
                List<Integer> in=new ArrayList<Integer>();
                for(String s:result){
                    in.add(StringUtil.stringToInteger(s));
                }
                exampleCriteria.andAgenidIn(in);
            }
        }
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            exampleCriteria.andAgenidEqualTo(agentAccounts.getAgentid());
        }
        List<RunfastCustom> list=runfastCustomService.selectByExampleWithPageable(example,pageable);
        Integer total=runfastCustomService.countByExample(example);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /**
     * 客服列表
     */
    @GetMapping("/select")
    public Result select(){

       return null;
        //return Result.ok(null,list);
    }

    /**
     * 保存客服
     */
    @PostMapping("/save")
    public Result save(@RequestBody  RunfastCustom custom){
        if(custom!=null && custom.getAgentIds()!=null && StringUtils.isNotBlank(custom.getMobile()) &&  StringUtils.isNotBlank(custom.getQrcode())){
            WaiMaiRunfastAgentbusiness agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(custom.getAgentIds());
            if(agentbusiness!=null){
                custom.setAgenid(agentbusiness.getId());
                custom.setAgenname(agentbusiness.getName());
            }else{return Result.fail(null,"找不到对应的代理商！");}
            custom.setType(0);
            custom.setCreatetime(new Date());
            runfastCustomService.insertSelective(custom);
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

    /**
     * 修改客服
     */
    @PostMapping("/update")
    public Result update(@RequestBody RunfastCustom custom){
        if(custom!=null && custom.getId()!=null && custom.getQrcode()!=null && custom.getMobile()!=null && custom.getAgenid()!=null){
            RunfastCustom  custom1=runfastCustomService.selectByPrimaryKey(custom.getId());
            WaiMaiRunfastAgentbusiness agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(custom.getAgenid());
            if(agentbusiness!=null){
                custom1.setAgenid(agentbusiness.getId());
                custom1.setAgenname(agentbusiness.getName());
            }else{return Result.fail(null,"找不到对应的代理商！");}
            if(custom1!=null){
                custom1.setMobile(custom.getMobile());
                custom1.setQrcode(custom.getQrcode());
                runfastCustomService.updateByPrimaryKeySelective(custom);
            }else{return Result.fail(null,"找不到对应的客服！");}
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

    /**
     * 删除客服
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        if(id!=null){
            runfastCustomService.deleteByPrimaryKey(id);
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }



}
